//
//  RightMeunViewController.m
//  MINFOBBS
//
//  Created by juvid on 13-11-11.
//  Copyright (c) 2013年 minfo. All rights reserved.
//

#import "RightMeunViewController.h"
#import "MFSideMenuContainerViewController.h"
#import "SideMeunViewController.h"
#import "ChangPhone.h"
#import "SBJson.h"
#import "DetectionSystemVersion.h"
#import "UIColor+expanded.h"
#import "MainSearchViewController.h"
@interface RightMeunViewController ()

@end

@implementation RightMeunViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([[DetectionSystemVersion alloc]Version]) {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    self.navigationItem.titleView=[[DetectionSystemVersion alloc]SetVieTitle:@"使用帮助举例" Fram:CGRectMake(5, 28, self.view.frame.size.width, 28) ImageName:@"bangzhu"];
    self.tabel.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tabel.backgroundColor=[UIColor colorWithHexString:@"0x434343"];
    self.tabelAlr.separatorColor=[UIColor whiteColor];
    arrRight=[[NSArray alloc]init];
    [self.vieTable.layer setCornerRadius:5.0];
    [self GetDatas:1];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)viewDidAppear:(BOOL)animated{
    if (![[DetectionSystemVersion alloc]Version]) {
        self.tabel.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-20);
    }
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [tRequest clearDelegatesAndCancel];
    [tRequest cancel];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [NSThread cancelPreviousPerformRequestsWithTarget:self];
}
#pragma mark 异步数据请求
-(void)GetDatas:(int)Indexs{
    
    tRequest=[[[ScreenAccess alloc]init]ASIGetNewsByGroupID:1];
    [tRequest setDelegate:self];
    [tRequest setUsername:@"allMessage"];
}

-(void)requestFinished:(ASIHTTPRequest*)request{
    NSString *response = [request responseString];
    
    SBJsonParser * parser = [[SBJsonParser alloc] init];
    NSMutableDictionary *Result = [parser objectWithString:response];
   
    if ([request.username isEqual:@"allMessage"]){
        BOOL IsSuccess=[[[Result objectForKey:@"response"]objectForKey:@"status"]integerValue]==200?YES:NO;
        if (IsSuccess==YES) {
            if (arrReturn==nil) {
                arrReturn=[[NSMutableArray alloc]init];
        }
             arrReturn=[[Result objectForKey:@"response"] objectForKey:@"help"];
            for (int i=0; i<arrReturn.count; i++) {
                if ([[[arrReturn objectAtIndex:i]objectForKey:@"filtername"] isEqual:@"软件下载"]) {
                    [arrReturn removeObjectAtIndex:i];
                }
                if ([[[arrReturn objectAtIndex:i]objectForKey:@"filtername"] isEqual:@"我的位置"]) {
                    [arrReturn removeObjectAtIndex:i];
                }
            }
            [self.tabel reloadData];
           
        }
        else{
        }
    }
  
    
}

-(void)requestFailed:(ASIHTTPRequest*)request{
   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    if ([tableView isEqual:self.tabelAlr]) {
        return 1;
    }
    else{
    // Return the number of sections.
    return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    if ([tableView isEqual:self.tabelAlr]) {
        return arrRight.count;
    }
    else{

    return arrReturn.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   
    if ([tableView isEqual:self.tabelAlr]) {
        static NSString *CellIdentifier = @"CellAlr";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle=UITableViewCellSelectionStyleGray;

        }
        cell.textLabel.text=[[arrRight objectAtIndex:indexPath.row] objectForKey:@"value"];
        
        UIView *vie=[[UIView alloc]init];
        cell.textLabel.backgroundColor=[UIColor colorWithHexString:@"0xf3f3f3"];
        cell.backgroundView=vie;
        cell.textLabel.textColor=[UIColor colorWithHexString:@"0x797979"];
        vie.backgroundColor=[UIColor colorWithHexString:@"0xf3f3f3"];
        cell.textLabel.font=[UIFont systemFontOfSize:14];
        return cell;
    }
    else{
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.backgroundColor=[UIColor clearColor];
         cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
         cell.selectionStyle=UITableViewCellSelectionStyleGray;
//        cell.accessoryView.backgroundColor=[UIColor whiteColor];
    }
     cell.imageView.image=[UIImage imageNamed:[[arrReturn objectAtIndex:indexPath.row] objectForKey:@"filtername"]];
    cell.textLabel.text=[[arrReturn objectAtIndex:indexPath.row] objectForKey:@"filtername"];
    cell.textLabel.textColor=[UIColor whiteColor];
    cell.textLabel.font=[UIFont systemFontOfSize:15];
        UIImageView *imgLine=[[UIImageView alloc]initWithFrame:CGRectMake(0,43,self.view.frame.size.width   , 1)];
        imgLine.image=[UIImage imageNamed:@"leftCellLine"];
        //
        [cell.contentView addSubview:imgLine];
    // Configure the cell...
        return cell;
    }
    
}
#pragma mark Table Delegate Methods
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if ([tableView isEqual:self.tabelAlr]) {
        return 41;

    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if ([tableView isEqual:self.tabelAlr]) {
        UIView *result = nil;
        result = [[UIView alloc] initWithFrame:CGRectZero];
        result.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"rightTableDang"]];
        UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(8, 12, 17, 17)];
        img.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@1",strAlrTitle]];
        [img setClipsToBounds:YES];
        [img setContentMode:UIViewContentModeScaleAspectFit];
        [result addSubview:img];
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(33, 0, self.tabelAlr.frame.size.width-33, 41)];
        lab.text=strAlrTitle;
        lab.backgroundColor=[UIColor clearColor];
        lab.textColor=[UIColor whiteColor];
        lab.font=[UIFont systemFontOfSize:16];
        [result addSubview:lab];
        return result;
        
    }
    return nil;
   
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/
- (MFSideMenuContainerViewController *)menuContainerViewController {
//    NSLog(@"第一个%@ 第二个%@",self.parentViewController,self.navigationController.parentViewController);
    return (MFSideMenuContainerViewController *)self.navigationController.parentViewController;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([tableView isEqual:self.tabelAlr]) {
        UINavigationController *navControl=[self.navigationController.parentViewController.childViewControllers objectAtIndex:1];
         [self HidePick:self.vieAlr];
        if (navControl.childViewControllers.count==1) {
            MainSearchViewController *mainSearch=[[MainSearchViewController alloc]init];
            mainSearch.strSearch=[[arrRight objectAtIndex:indexPath.row] objectForKey:@"value"];
            [navControl pushViewController:mainSearch animated:YES ];
        }
        else if(navControl.childViewControllers.count==2){
            MainSearchViewController *mainSearch=(MainSearchViewController *)[navControl .childViewControllers objectAtIndex:1];
            [mainSearch GetDatas:[[arrRight objectAtIndex:indexPath.row] objectForKey:@"value"]];
        }
        else{
            MainSearchViewController *mainSearch=(MainSearchViewController *)[navControl .childViewControllers objectAtIndex:1];
            [mainSearch GetDatas:[[arrRight objectAtIndex:indexPath.row] objectForKey:@"value"]];
            [navControl popToViewController:mainSearch animated:YES];
        }
        [self.menuContainerViewController setMenuState:MFSideMenuStateClosed];
    }
    else{
        UIWindow* window = [UIApplication sharedApplication].keyWindow;
        if (!window)
        {
            window = [[UIApplication sharedApplication].windows objectAtIndex:0];
        }
        self.vieAlr.frame = window.frame;
        //    [modalBackgroundView addSubview:self];
        //    [window addSubview:self.vieAlr];
        [self.menuContainerViewController.view addSubview:self.vieAlr];
        arrRight=[[arrReturn objectAtIndex:indexPath.row] objectForKey:@"values"];
        self.vieTable.frame=CGRectMake(0, 0, self.vieTable.frame.size.width, 46+44*arrRight.count);
        self.vieTable.center=self.vieAlr.center;
        strAlrTitle=[[arrReturn objectAtIndex:indexPath.row] objectForKey:@"filtername"];
        [self.tabelAlr reloadData];
        [self ShowPick:self.vieAlr ];
        
    }
   
   
}
#pragma mark 动画
-(void)HidePick:(UIView *)vieBounced {
    [UIView animateWithDuration:0.3
                     animations:^(void) {
                         vieBounced.alpha = 0.0;
//                         vieBounced.frame = CGRectMake(0, self.view.frame.size.height, 104, (self.view.frame.size.height-117-8)/3);
                     } completion:^(BOOL finished) {
                         
                         [vieBounced removeFromSuperview];
                     }];
    
}
-(void)ShowPick:(UIView *)vieBounced{
//    const CGRect toFrame = (CGRect){arrowPoint, 104, ((self.view.frame.size.height-117-8)/3)};
    
    [UIView animateWithDuration:0.3
                          delay:0
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^(void){
                         vieBounced.alpha = 1.0;
//                         vieBounced.frame = toFrame;
                     }
     
                     completion:^(BOOL finished) {
                         
                         //                                  [image removeFromSuperview];
                     }];
    //    [UIView animateWithDuration:showTime
    //                     animations:^(void) {
    //                         image.alpha = 1.0;
    //                         image.frame = toFrame;
    //                     }];
    
}

- (void)setupMenuBarButtonItems {
   
   
    //    else {
    //        self.navigationItem.leftBarButtonItem = [self leftMenuBarButtonItem];
    //    }
}
- (IBAction)HideVie:(id)sender {
     [self HidePick:self.vieAlr];
}
@end
